<template>
<el-dialog
    :title="title"
    custom-class="rs-dialog"
    :visible.sync="dlgVisible"
    :close-on-click-modal="false"
    :before-close="()=>handleClose('cancel')"
    width="500px">
    <el-form :model="form" :rules="rules" :label-width="formLabelWidth" ref="form">
        <el-form-item label="条目值" prop="tmbh" >
            <el-input v-model="form.tmbh" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="条目名称" prop="tmmc">
            <el-input v-model="form.tmmc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="条目序号" prop="tmxh">
            <el-input v-model.number="form.tmxh" autocomplete="off"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose('cancel')">取 消</el-button>
        <el-button type="primary" @click="handleClose('ok')">确 定</el-button>
    </span>
</el-dialog>
</template>
<script>
import deepmerge from 'deepmerge';
import dlg from '../../mixin/dlg';
import dictApi from '@/api/dict';
export default {
    name:'InfoForm',
    mixins:[dlg],
    data(){
        return{
            formLabelWidth: '80px',
            form:{
                tmzj:'',
                zdbh:'',
                tmbh:'',
                tmmc:'',
                tmpbh:'',
                tmxh:0,
                tmzt:0
            },
            rules: {
                tmbh:[
                    {required: true, message: '请输入条目序号'}
                ],
                tmmc:[
                    {required: true, message: '请输入条目名称'}
                ],
                tmxh:[
                    {required: true, message: '请选择条目序号'}
                ]
            },
            clk:null
        }
    },
    methods:{
        onShow(bean){
            this.$refs.form && this.$refs.form.resetFields();
            const {form} = this;
            form.zdbh = bean.zdbh;
            if( bean.tmzj ){
                this.title = "编辑"
                form.tmzj = bean.tmzj;
                form.tmbh = bean.tmbh;
                form.tmmc = bean.tmmc;
                form.tmpbh = bean.tmpbh;
                form.tmxh = bean.tmxh;

            }else{
                this.title = "新建"
                form.tmzj = "";
                form.tmbh = "";
                form.tmmc = "";
                form.tmxh = 0;
                form.tmpbh = "";
            }
        },
        onOk(){
            const {form,unit} = this;
            const data = deepmerge({},form);
            this.$refs.form.validate((valid)=>{

                if(!valid) return;
            
                dictApi.saveDictInfo(data)
                .then(response => {
                    //console.log(response)
                    const { data } = response;
                    if( data.error_code == 0 ){
                        this.closeDialog('ok');
                    }else{
                        this.$message({
                            message: data.error_code==20701?'条目已存在':data.error_desc,
                            type: 'warning'
                        });
                    }
                }).catch(error => {
                    this.$message({message: '本地异常',type: 'warning'});
                });
            });
        }
    }
}
</script>